(ns org.mikrosopht.movie
  (:use [clojure.contrib.command-line :only (with-command-line)]
        [clojure.contrib.duck-streams :only (reader with-in-reader)]
        [clojure.contrib.json :only (read-json print-json)])
  (:import (java.net URLEncoder)))

(def rotten-search-prefix "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=hh2m6efbg9spnprrvs4uh6u8&q=")

(defn rotten-score [movie-name]
  (let [search-url (apply str [rotten-search-prefix (URLEncoder/encode movie-name "UTF-8")])]
    (with-in-reader (reader search-url)
      (with-open [rdr *in*]
        (get-in (first ((read-json rdr) :movies)) [:ratings :critics_score])))))

(dorun (map #(println (rotten-score %)) *command-line-args*))
